﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolrExample.Data;
using System.Configuration;
using SolrExample.Common.Helpers;
using SolrExample.Interface.DomainObject;

namespace SolrExample.Tests
{
    public class JobServices
    {
        public string ConnectionString { get; set; }
        public string ConnectionProviderName { get; set; }
        public string CountryCode { get; set; }

        public void CommitIndex() {
            JobAdDao jobAdDao = new JobAdDao(ConnectionString, ConnectionProviderName, CountryCode);

            IList<long> jobIds = jobAdDao.GetJobIndex();
            int bathSize = int.Parse(ConfigurationManager.AppSettings["BathSize"]);
            IList<IList<long>> jobIdBatchs = CommonHelper.PageDataList(jobIds, bathSize);

            Console.WriteLine("JobIds Count: " + jobIds.Count);

            int batchCount = 0;
            foreach (IList<long> jobIdBatch in jobIdBatchs) {
                batchCount++;

                IList<IJobAd> jobAdBatch = new List<IJobAd>();
                try
                {
                    foreach (long jobId in jobIdBatch)
                    {
                        IJobAd jobAd = jobAdDao.GetJobAdDetails(jobId);
                        if (null != jobAd && jobAd.IsSearchableRecord)
                        {
                            jobAdBatch.Add(jobAd);
                        }
                    }
                }
                catch { 
                    Console.WriteLine("Get job detail from jobsdb failure. JobIds: " + string.Join(", ", jobIdBatch.Select(i=>i.ToString()).ToArray())); 
                }
                Console.WriteLine("Valid Job Count: " + jobAdBatch.Count);
            }
        }
    }
}
